package cn.edu.scau.cmi.ema.controller;

import cn.edu.scau.cmi.ema.base.CmiPagination;
import cn.edu.scau.cmi.ema.base.CmiEmaController;
import cn.edu.scau.cmi.ema.domain.Productcode;
import cn.edu.scau.cmi.ema.util.CmiHibernateProxyUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Set;

@Controller("ProductcodeController")
public class ProductcodeController extends CmiEmaController {

	@RequestMapping("/listProductcodes")
	public ModelAndView listProductcodes(HttpServletRequest request, @ModelAttribute CmiPagination pagination) {
		ModelAndView mav = new ModelAndView();

		pagination = productcodePaginationService.updatePagination(request, pagination);
		Set<Productcode> productcodes = productcodePaginationService.getPaginationProductcodes(pagination);

		mav.addObject("productcodes", productcodes);
		mav.addObject("entityName", "productcode");
		mav.addObject("pagination", pagination);
		mav.setViewName("productcode/listProductcodes");
		return mav;
	}

	public String listProductcodes() {
		return "redirect:/listProductcodes";
	}

	@RequestMapping("/viewProductcode")
	public ModelAndView viewProductcode(@RequestParam Integer productcodeId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("productcode", productcodeDAO.findProductcodeByPrimaryKey(productcodeId));
		mav.setViewName("productcode/viewProductcode");
		return mav;
	}

	@RequestMapping(value = "/confirmDeleteProductcodes")
	public ModelAndView confirmDeleteProductcodes(
			@RequestParam(value = "deleteProductcodesIds", required = true) Set<Integer> deleteProductcodesIds) {
		ModelAndView mav = new ModelAndView();

		Set<Productcode> deleteProductcodes = productcodeDAO.findProductcodesByIds(deleteProductcodesIds);
		Set<Productcode> referencedProductcodes = productcodeService.loadReferenceProductcodes(deleteProductcodes);
		boolean hasReferenced;
		if (referencedProductcodes.size() == 0) {
			mav.addObject("deleteEntitys", deleteProductcodes);
			hasReferenced = false;
		} else {
			deleteProductcodes.removeAll(referencedProductcodes);
			mav.addObject("referencedEntitys", referencedProductcodes);
			mav.addObject("deleteEntitys", deleteProductcodes);
			hasReferenced = true;
		}
		mav.addObject("entityName", "Productcode");
		mav.addObject("hasReferenced", hasReferenced);
		mav.setViewName("forward:/listProductcodes");
		return mav;
	}

	@RequestMapping("/deleteProductcode")
	public String deleteProductcode(@RequestParam Integer productcodeId) {
		Productcode productcode = productcodeDAO.findProductcodeByPrimaryKey(productcodeId);
		productcodeService.deleteProductcode(productcode);
		return "forward:/listProductcodes";
	}

	@RequestMapping("/confirmDeleteProductcode")
	public ModelAndView confirmDeleteProductcode(@RequestParam Integer productcodeId) {

		ModelAndView mav = new ModelAndView();

		mav.addObject("productcode", productcodeDAO.findProductcodeByPrimaryKey(productcodeId));

		mav.setViewName("productcode/deleteProductcode");

		return mav;
	}

	@RequestMapping("/deleteProductcodes")
	public String deleteProductcodes(
			@RequestParam(value = "deleteProductcodesId", required = true) Set<Integer> deleteProductcodesId) {
		productcodeService.deleteProductcodes(deleteProductcodesId);
		return "forward:/listProductcodes";
	}

	@RequestMapping("/editProductcode")
	public ModelAndView editProductcode(@RequestParam Integer productcodeId) {

		ModelAndView mav = new ModelAndView();

		Productcode productcode = productcodeDAO.findProductcodeByPrimaryKey(productcodeId);
		// 将需要编辑的对象加到mav对象中
		mav.addObject("productcode", productcode);

		// 把所有的引用productcode对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(productcodeService.loadProductcodeObjectReferencedSet(productcode));

		// 将编辑页面productcode添加到mav中
		mav.setViewName("productcode/editProductcode");
		return mav;
	}

	@RequestMapping("/newProductcode")
	public ModelAndView newProductcode() {
		ModelAndView mav = new ModelAndView();
		Productcode productcode = new Productcode();

		mav.addObject("productcode", productcode);

		// 把所有的引用productcode对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(productcodeService.loadProductcodeObjectReferencedSet(productcode));

		mav.addObject("newFlag", true);
		mav.setViewName("productcode/editProductcode");

		return mav;
	}

	@RequestMapping("/saveProductcode")
	public ModelAndView saveProductcode(@ModelAttribute Productcode productcode) {

		ModelAndView mav = new ModelAndView();

		Productcode existingProductcode = CmiHibernateProxyUtil
				.deproxy(productcodeDAO.findProductcodeByPhysicalPrimaryKey(productcode.getName()));

		// 新建记录
		if (productcode.getId() == null) {
			if (productcode.equals(existingProductcode)) {

				mav.addAllObjects(productcodeService.loadProductcodeClassReferencedSet(productcode));
				// mav = editContinue(mav, productcode);
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("productcode/editProductcode");
			} else {
				productcodeService.saveProductcode(productcode);
				mav.setViewName("forward:/listProductcodes");
			}
		}
		// 编辑记录
		else {
			Productcode originProductcode = CmiHibernateProxyUtil
					.deproxy(productcodeDAO.findProductcodeByPrimaryKey(productcode.getId()));
			if (!productcode.equals(originProductcode) && productcode.equals(existingProductcode)) {
				mav.addAllObjects(productcodeService.loadProductcodeClassReferencedSet(productcode));
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("productcode/editProductcode");
			} else {
				productcodeService.saveProductcode(productcode);
				mav.setViewName("forward:/listProductcodes");
			}
		}
		return mav;
	}

	@RequestMapping("/productcodeController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request,
			@ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

}
